package cn.liyunqi.ttv.web.controller;

import cn.liyunqi.core.exception.BizException;
import cn.liyunqi.core.util.StringUtil;
import cn.liyunqi.core.util.UUIDUtil;
import cn.liyunqi.ttv.web.bo.UserInfo;
import cn.liyunqi.ttv.web.common.RequestMap;
import cn.liyunqi.ttv.web.common.ResponseMap;
import cn.liyunqi.ttv.web.service.MailService;
import cn.liyunqi.ttv.web.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private MailService mailService;

    /**
     * 获取邮箱注册验证嘛
     * @return
     */
    @RequestMapping(value = "/getMail/register",method = RequestMethod.POST)
    public @ResponseBody
    Object getMail(RequestMap requestMap){
        ResponseMap<String,Object> responseMap = new ResponseMap<String,Object>();
        try {

            String mail = requestMap.getStringValue("mail","邮箱",RequestMap.REQUIRED);
            Integer type = requestMap.getIntegerValue("type","类型",RequestMap.REQUIRED);

            mailService.sendMail(type,mail);
        } catch (BizException e){
            responseMap.returnError(e);
            //e.printStackTrace();
        } catch (Exception e){
            responseMap.returnError();
            e.printStackTrace();
        }
        return responseMap;
    }
    /**
     * 注册验证嘛
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public @ResponseBody
    Object register(RequestMap requestMap){
        ResponseMap<String,Object> responseMap = new ResponseMap<String,Object>();
        try {

            String mail = requestMap.getStringValue("mail","邮箱",RequestMap.REQUIRED);
            String mailCode = requestMap.getStringValue("mailCode","邮箱验证码",RequestMap.REQUIRED);
            String password = requestMap.getStringValue("password","密码",RequestMap.REQUIRED);
            String recommendMail = requestMap.getStringValue("recommendMail","推荐人邮箱",RequestMap.NOTREQUIRED);

            userInfoService.register(mail,mailCode,password,recommendMail);
        } catch (BizException e){
            responseMap.returnError(e);
            //e.printStackTrace();
        } catch (Exception e){
            responseMap.returnError();
            e.printStackTrace();
        }
        return responseMap;
    }

    /**
     * 登陆
     * @param requestMap
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public @ResponseBody
    Object login(RequestMap requestMap){
        ResponseMap responseMap = new ResponseMap();
        try {

            String mail = requestMap.getStringValue("mail","邮箱",RequestMap.REQUIRED);
            String password = requestMap.getStringValue("password","密码",RequestMap.REQUIRED);

            UserInfo vo = userInfoService.login(mail,password);

            responseMap.put("vo",vo);


        } catch (BizException e) {
            responseMap.returnError(e);
        }catch (Exception ex){
            responseMap.returnError();
        }
        return responseMap;
    }

    /**
     * 重置密码
     * @return
     */
    @RequestMapping(value = "/resetPassword",method = RequestMethod.POST)
    public @ResponseBody
    Object resetPassword(RequestMap requestMap){
        ResponseMap<String,Object> responseMap = new ResponseMap<String,Object>();
        try {

            String mail = requestMap.getStringValue("mail","邮箱",RequestMap.REQUIRED);
            String mailCode = requestMap.getStringValue("mailCode","邮箱验证码",RequestMap.REQUIRED);
            String password = requestMap.getStringValue("password","密码",RequestMap.REQUIRED);

            userInfoService.resetPassword(mail,mailCode,password);
        } catch (BizException e){
            responseMap.returnError(e);
            //e.printStackTrace();
        } catch (Exception e){
            responseMap.returnError();
            e.printStackTrace();
        }
        return responseMap;
    }
    /**
     * 验证token
     * @return
     */
    @RequestMapping(value = "/checkAuth",method = RequestMethod.GET)
    public @ResponseBody
    Object checkAuth(HttpServletRequest request){
        ResponseMap<String,Object> responseMap = new ResponseMap<String,Object>();
        try {

            String authorization = request.getHeader("Authorization");

            if(StringUtil.isEmptyOrNull(authorization)){
                throw new BizException("会员登录异常，请重新登录","0501");
            }

            String param[] = authorization.split("_");
            if(param.length != 2){
                throw new BizException("会员登录异常，请重新登录","0501");
            }

            String token = userInfoService.checkAuth(param[0],param[1]);
            responseMap.put("token",token);
            responseMap.put("userId",param[0]);
        } catch (BizException e){
            responseMap.returnError(e);
            //e.printStackTrace();
        } catch (Exception e){
            responseMap.returnError();
            e.printStackTrace();
        }
        return responseMap;
    }

}
